using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class Constellation : EveObject
    {
        private int regionId;
        private string constellationName;
        private double x, y, z;
        private double xMin, yMin, zMin;
        private double xMax, yMax, zMax;
        private int factionId;
        private double radius;
        private DateTime sovereigntyDateTime; // rev 2.0 only
        private int allianceId; // rev 2.0 only
        private DateTime graceDateTime; // rev 2.0 only

        private Region region;
        private Faction faction;
        private Vector location;
        private Vector minimum;
        private Vector maximum;

        private List<Constellation> jumps;

        public string Name 
        { 
            get { return constellationName; } 
        }
        public Vector Location
        {
            get
            {
                if (location == null)
                    location = new Vector(x, y, z);
                return location;
            }
        }
        public Vector Minimum
        {
            get
            {
                if (minimum == null)
                    minimum = new Vector(xMin, yMin, zMin);
                return minimum;
            }
        }
        public Vector Maximum
        {
            get
            {
                if (maximum == null)
                    maximum = new Vector(xMax, yMax, zMax);
                return maximum;
            }
        }
        public double Radius
        {
            get { return radius; }
        }
        public Region Region 
        { 
            get 
            {
                if (region == null)
                    region = new Region(db, regionId);
                return region;
            } 
        }
        public Faction Faction
        {
            get
            {
                if (faction == null)
                    faction = new Faction(db, factionId);
                return faction;
            }
        }
        public DateTime SovereigntyDateTime // rev 2.0 only
        {
            get { return sovereigntyDateTime; }
        }
        public int AllianceId // rev 2.0 only
        {
            get { return allianceId; }
        }
        public DateTime GraceDateTime // rev 2.0 only
        {
            get { return graceDateTime; }
        }

        public List<Constellation> Jumps
        {
            get
            {
                if (jumps == null)
                {
                    jumps = new List<Constellation>();
                    string query = "SELECT * FROM mapConstellationJumps WHERE fromConstellationID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        jumps.Add(new Constellation(db, row["toConstellationID"]));
                }
                return jumps;
            }
        }

        internal Constellation(DbDataRow dr)
            : base(dr.Database, EveObjectType.Constellation, dr["constellationID"])
        {
            Fill(dr);
        }
        internal Constellation(Database db, int id) 
            : base(db, EveObjectType.Constellation, id) 
        {
            Fill(db.ExecuteSingle("SELECT * FROM mapConstellations WHERE constellationID=" + id));
        }

        private void Fill(DbDataRow dr)
        {
            regionId = dr["regionID"];
            constellationName = dr["constellationName"];
            x = dr["x"];
            y = dr["y"];
            z = dr["z"];
            xMin = dr["xMin"];
            yMin = dr["yMin"];
            zMin = dr["zMin"];
            xMax = dr["xMax"];
            yMax = dr["yMax"];
            zMax = dr["zMax"];
            factionId = dr["factionID"];
            radius = dr["radius"];
            sovereigntyDateTime = dr["sovereigntyDateTime"];
            allianceId = dr["allianceID"];
            graceDateTime = dr["graceDateTime"];
        }
    }
}
